package com.sigerap.voucher.service;

//import java.awt.List;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import com.sigerap.common.util.Utilitario;
import com.sigerap.voucher.model.Voucher;
import com.sigerap.voucher.model.dao.VoucherDAO;
import com.sigerap.voucher.model.dao.jpa.JPAVoucherDAO;
/**
 * 
 * @author Spirit-20121
 *
 */

public class GenerarVoucherService {
	
	private EntityManager em;
	private VoucherDAO voucherDAO;
	
	public GenerarVoucherService(){
		em = Utilitario.getInstance().getEntityManager();
		voucherDAO = new JPAVoucherDAO(em);
	}
	
	public List<Voucher> listarVoucheres(){
		return voucherDAO.findAll();
	}
	
	
	public Voucher generarVoucher(Voucher v){
		Voucher newVoucher=null;
		EntityTransaction tx= em.getTransaction();
		
				
		try{
			tx.begin();
			v.setTipoTarjeta(v.getTipoTarjeta().substring(0, 1));
			voucherDAO.insert(v);
			
			v.setTipoMoneda(v.getTipoMoneda().substring(0, 1));
			voucherDAO.insert(v);
			tx.commit();
			
		}catch(Exception ex){
			ex.printStackTrace();
			tx.rollback();
			throw new RuntimeException("No se pudo registrar voucher");
		}
		return newVoucher;
	}
	
}
